#! /usr/bin/python
# _*_ coding: utf-8 _*_

'a mod of class Block'

__author__ = 'alanpo'

import enums


#像素块类
class Pixel:
    def __init__(self, x, y, xs, ys):
        self.iswhat = enums.IsWhat.Block  #类别
        self.nextway = enums.Direction.Stop  #下一步
        self.loc = [x, y]  #位置坐标
        self.follow = [x, y]  #跟随块位置坐标
        self.size = [xs, ys]  #地图大小

    #刷新跟随坐标follow,遇墙穿墙
    def reset_follow(self):
        x = self.loc[0]
        y = self.loc[1]
        self.follow = [x, y]
        if self.nextway == enums.Direction.Left:
            self.follow[1] -= 1
            if self.follow[1] == 0:
                self.follow[1] = self.size[1] - 2  #穿墙
        elif self.nextway == enums.Direction.Right:
            self.follow[1] += 1
            if self.follow[1] == self.size[1] - 1:
                self.follow[1] = 1
        elif self.nextway == enums.Direction.Up:
            self.follow[0] -= 1
            if self.follow[0] == 0:
                self.follow[0] = self.size[0] - 2
        elif self.nextway == enums.Direction.Down:
            self.follow[0] += 1
            if self.follow[0] == self.size[0] - 1:
                self.follow[0] = 1

    #设置各类块
    def set_block(self):
        self.iswhat = enums.IsWhat.Block
        self.nextway = enums.Direction.Stop
        self.follow = self.loc

    def set_framel(self):
        self.iswhat = enums.IsWhat.Framel
        self.nextway = enums.Direction.Stop
        self.follow = self.loc

    def set_framer(self):
        self.iswhat = enums.IsWhat.Framer
        self.nextway = enums.Direction.Stop
        self.follow = self.loc

    def set_framexy(self):
        self.iswhat = enums.IsWhat.Framexy
        self.nextway = enums.Direction.Stop
        self.follow = self.loc

    def set_food(self):
        self.iswhat = enums.IsWhat.Food
        self.nextway = enums.Direction.Stop
        self.follow = self.loc

    #设置蛇
    def set_tail(self, way: enums.Direction):
        self.nextway = way
        self.iswhat = enums.IsWhat.Tail
        self.reset_follow()

    def to_tail(self):
        self.iswhat = enums.IsWhat.Tail
        self.reset_follow()

    def set_body(self, way: enums.Direction):
        self.nextway = way
        self.iswhat = enums.IsWhat.Body
        self.reset_follow()

    def to_body(self):
        self.iswhat = enums.IsWhat.Body
        self.reset_follow()

    def set_head(self, way: enums.Direction):
        self.nextway = way
        self.iswhat = enums.IsWhat.Head
        self.reset_follow()

    #改写移动方向
    def set_way(self, way: enums.Direction):
        self.nextway = way
        self.reset_follow()

    #打印当前块
    def prt(self):
        self.iswhat.print_it()

    #获取当前位置
    def get_loc(self):
        return self.loc


#test
if __name__ == "__main__":
    x = Pixel(1, 1, 2, 2)
    print(x.iswhat, x.nextway, x.loc, x.follow, x.size)
